package cn.tedu.csmall.stock.webapi.quartz;

import cn.tedu.csmall.commons.pojo.stock.model.StockTb;
import cn.tedu.csmall.stock.service.IStockService;
import cn.tedu.csmall.stock.webapi.quartz.config.RabbitMQConfig;
import cn.tedu.csmall.stock.webapi.quartz.config.RabbitStockConfig;
import cn.tedu.csmall.stock.webapi.utils.RedisBloomUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;

public class QuartzJob implements Job {

    // RabbitTemplate就是amqp协议框架提供的发送消息的对象
    @Autowired
    private RabbitTemplate rabbitTemplate;

//    public QuartzJob(){
//        System.out.println("实例化的新的对象");
//    }
    @Autowired
    private RedisBloomUtils redisBloomUtils;

    static int i=1;
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        // 输出当前时间
        System.out.println("--------------"+ LocalDateTime.now()+"---------------");

        // 先定义要向布隆过滤器中保存的元素
        String[] colors={"red","origin","yellow","green","blue","pink","white"};
        // 向布隆过滤器中保存这些元素
        redisBloomUtils.bfmadd("colorBloom",colors);
        // 定义一个要检查的字符
        String elm="gray";
        // 输出判断结果
        System.out.println("判断"+elm+"是否在布隆过滤器中:"+
                            redisBloomUtils.bfexists("colorBloom",elm));


        // 先简单的发送一个字符串
        // convertAndSend([交换机名称],[路由Key名称],[要发送的消息])
        //rabbitTemplate.convertAndSend(RabbitMQConfig.STOCK_EX,
        //       RabbitMQConfig.STOCK_ROUT,"消息:执行减少库存的操作");
        /*StockTb stock=new StockTb();
        stock.setId(i++);
        stock.setCommodityCode("PC100");
        stock.setReduceCount(RandomUtils.nextInt(10)+1);
        rabbitTemplate.convertAndSend(RabbitStockConfig.STOCK_TB_EX,
                RabbitStockConfig.STOCK_TB_ROUT,stock);*/
    }
}
